<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'DAOPais.php');
require_once(JPATH_COMPONENT.DS.'persistencia'.DS.'tables'.DS.'pais.php');
require_once(JPATH_COMPONENT.DS.'dominio'.DS.'DTOFactory.php');

class JDAOPais implements DAOPais{
	
	public function getPaisIdByName($name) {
		$pais = $this -> singleObjectQuery("SELECT idPais FROM #__pais WHERE nombrePais='".$name."'");	
		if($pais!=null)	
			return $pais->idPais;
		return null;
	}

	public function savePais($dtoPais) {
		$factoria=DTOFactory::getInstance();
			
		$row=$factoria->fromDTOPais2PaisTable($dtoPais);
					
		if(!$row->store()){
	        JError::raiseError(500, $row->getError());
	    }
	    return $factoria->fromPaisTable2DTOPais($row);
	}

	private function singleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery( $query );
		$row = $db->loadObject();
		if ($db->getErrorNum()) {
			echo $db->stderr();
			return false;
		}
		return $row;
	}
	private function multipleObjectQuery($query){
		$db =& JFactory::getDBO();
		$db->setQuery($query);
		return $db->loadObjectList();
	}
}
?>